# -*- coding:UTF-8 -*-

import os
import git.cmd
from git.repo import Repo
import datetime

# todo： 定义克隆远程仓库URL及本地目录
# REMOTE_URL = "http://账号:密码@192.168.10.8:7990/scm/bambooscript/test.git"
REMOTE_URL = "https://johnforrest:yin0503@gitee.com/johnforrest/mapboxdemo.git"
dir_path = os.path.join(r"E:\devcode", "mapboxdemo")
# 账号和密码
git_user = "johnforrest"
git_email = "571716604@qq.com"


# 判断仓库本地目录是否存在
def is_dir(path):
    result = os.path.isdir(path + os.sep + ".git")
    if not result:
        print("路径:%s \n是否存在:" % s % (path, result))
        return result
    else:
        print("路径:%s \n是否存在: %s" % (path, result))
        return result


# 获取对象。path是仓库到本地的地址
def git_object(path):
    # 获取repo对象。本地仓库地址
    repo = Repo(path)
    # 通过Repo对象获取git对象
    git = repo.git
    # 通过repo对象获取remote对象
    remote = repo.remote()
    return git, remote


# 克隆远程仓库到本地
def git_clone(remote_url, local_path):
    dir_msg = is_dir(dir_path)
    if not dit_msg:
        Repo.clone_from(remote_url, to_path=local_path)
        print("已克隆到本地")


# 添加提交用户 邮箱
def git_user(user, user_email):
    repo = Repo(dir_path)
    git = repo.git()
    git.config("--global", "user.name", "%s" % (user))
    git.config("--global", "user.email", "%s" % (user_email))


# pull-add-commit-push。 commit_msg表示提交信息
def git_pull(path, commit_msg):
    git, remote = git_object(path)
    remote.pull()
    # git.add('.')
    # git.commit('-m', '%s' % (commit_msg))
    # remote.push()


def git_push1(path, commit_msg):
    git, remote = git_object(path)
    remote.pull()
    git.add('.')
    git.commit('-m', '%s' % (commit_msg))
    remote.push()


if __name__ == '__main__':
    # git,remote=git_object(dir_path)
    # log_msg=git.log()
    # print (log_msg)
    # 克隆到本地
    # git_clone(REMOTE_URL, dir_path)
    # 统一认证
    dir_path1 = os.path.join(r"E:\devcode\kzsny\wmp", "wmp-uca-web-main")
    dir_path2 = os.path.join(r"E:\devcode\kzsny\wmp", "wmp-uca-web-ssoLogin")
    dir_path3 = os.path.join(r"E:\devcode\kzsny\wmp", "wmp-uca-web-usm")
    dir_path4 = os.path.join(r"E:\devcode\kzsny\wmp", "wmp-uca-web-login")
    # 全生命周期管理
    dir_path5 = os.path.join(r"E:\devcode\kzsny\wmp", "NewEnergy-web")
    dir_path6 = os.path.join(r"E:\devcode\kzsny\wmp", "NewEnergy-web-business")
    dir_path7 = os.path.join(r"E:\devcode\kzsny\wmp", "NewEnergy-web-usm")
    # 后台
    dir_path8 = os.path.join(r"E:\devcode\kzsny", "wmp-uca")
    dir_path9 = os.path.join(r"E:\devcode\kzsny", "NewEnergy")

    nowtime = datetime.datetime.now().strftime('%Y-%m-%d  %H:%M:%S')

    dirArr = [dir_path1, dir_path2, dir_path3, dir_path4, dir_path5, dir_path6, dir_path7, dir_path8, dir_path9]
    # dirArr=[dir_path6]
    # try:
    #     for xitem in dirArr:
    #         git_push(xitem, "yjw提交 " + nowtime)
    # finally:
    #     print('提交完成')
    try:
        for xitem in dirArr:
            git_pull(xitem, "yjw提交 " + nowtime)
        # 需要判断是否会抛出异常的代码，如果没有异常处理，python会直接停止执行程序
        # except:  # 这里会捕捉到上面代码中的异常，并根据异常抛出异常处理信息
    except Exception:  # 同时也可以接受异常名称和参数，针对不同形式的异常做处理
        # code  # 这里执行异常处理的相关代码，打印输出等
        # else:  # 如果没有异常则执行else
        # code  # try部分被正常执行后执行的代码
        print('拉取完成')
    # finally:
# code  # 退出try语句块总会执行的程序
